package com.resou.reader.mine.vipcenter.payview;

import android.content.Context;
import android.support.design.widget.BottomSheetDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.resou.reader.R;
import com.resou.reader.utils.PopWindowUtils;
import com.resou.reader.utils.ToastUtil;

/**
 * Author: 萤火虫
 * E-mail:  Allensuns@163.com
 * Description:
 * CreateTime: 2019-04 -17
 **/
public class PayBootomDialog {
    Context context;
    View view;
    String mAmount;//支付金额
    String mCurrency;//热币
    String mVoucher;//送热券

    BottomSheetDialog bottomSheetDialog;
    IPayMethodListener iPayMethodListener;


    public PayBootomDialog(Builder builder) {
        this.bottomSheetDialog = builder.bottomSheetDialog;
        this.iPayMethodListener = builder.iPayMethodListener;
        this.context = builder.context;
        this.mAmount = builder.mAmount;
        this.mCurrency = builder.mCurrency;
        this.mVoucher = builder.mVoucher;
        initView();
    }

    public void initView() {
        ViewHolder viewHolder;
        if (view == null) {
            viewHolder = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.dialog_payment_method, null);
            viewHolder.iv_back = view.findViewById(R.id.iv_back);
            viewHolder.tv_amount = view.findViewById(R.id.tv_amount);
            viewHolder.tv_currency = view.findViewById(R.id.tv_currency);
            viewHolder.tv_voucher = view.findViewById(R.id.tv_voucher);
            viewHolder.ll_ali_pay = view.findViewById(R.id.ll_ali_pay);
            viewHolder.ll_we_chat = view.findViewById(R.id.ll_we_chat);
            view.setTag(viewHolder);
            bottomSheetDialog.setContentView(view);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.tv_amount.setText("¥" + mAmount);
        viewHolder.tv_currency.setText(mCurrency + "热币");
        if (mVoucher != null && mVoucher.length() > 0) {
            viewHolder.tv_voucher.setVisibility(View.VISIBLE);
            viewHolder.tv_voucher.setText("送" + mVoucher + "热券");
        } else {
            viewHolder.tv_voucher.setVisibility(View.GONE);
        }
        if (mCurrency != null && mCurrency.length() > 0) {
            viewHolder.tv_currency.setVisibility(View.VISIBLE);
            viewHolder.tv_currency.setText(mVoucher + "热券");
        } else {
            viewHolder.tv_currency.setVisibility(View.GONE);
        }
        viewHolder.ll_ali_pay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (iPayMethodListener != null) {
                    iPayMethodListener.setIAliPayOnclickListener();
                }
            }
        });
        viewHolder.ll_we_chat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (iPayMethodListener != null) {
                    iPayMethodListener.setIWXPayOnClickListener();
                }
            }
        });
        viewHolder.iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtil.makeShortToast("取消支付");
                dissDialog();
            }
        });
    }

    /**
     * 显示弹窗
     */
    public void showDialog() {
        initView();
        if (!bottomSheetDialog.isShowing()) {
            bottomSheetDialog.show();
        }
    }

    /**
     * 隐藏弹框
     */
    public void dissDialog() {
        if (bottomSheetDialog.isShowing()) {
            bottomSheetDialog.dismiss();
        }
    }

    /**
     * builder模式
     */
    public static class Builder {
        Context context;
        String mAmount;//支付金额
        String mCurrency;//热币
        String mVoucher;//送热券
        BottomSheetDialog bottomSheetDialog;
        IPayMethodListener iPayMethodListener;

        public Builder(Context context) {
            this.context = context;
            bottomSheetDialog = new BottomSheetDialog(context);
        }

        public Builder setmAmount(String mAmount) {
            this.mAmount = mAmount;
            return this;
        }

        public String getmCurrency() {
            return mCurrency;
        }

        public Builder setiPayMethodListener(IPayMethodListener iPayMethodListener) {
            this.iPayMethodListener = iPayMethodListener;
            return this;
        }

        public Builder setmCurrency(String mCurrency) {
            this.mCurrency = mCurrency;
            return this;
        }

        public String getmVoucher() {
            return mVoucher;
        }

        public Builder setmVoucher(String mVoucher) {
            this.mVoucher = mVoucher;
            return this;
        }

        public PayBootomDialog create() {
            return new PayBootomDialog(this);
        }
    }


    class ViewHolder {
        ImageView iv_back;
        TextView tv_amount;//支付金额
        TextView tv_currency;//热币
        TextView tv_voucher;//送热券
        LinearLayout ll_ali_pay;//支付宝
        LinearLayout ll_we_chat;//微信
    }
}
